<?php
namespace Action;
use HY\Action;
!defined('HY_PATH') && exit('HY_PATH not defined.');
class SetIp extends HYBBS {
	public function __construct(){
		parent::__construct();

	}
	public function setIp(){
        $ip= trim(X('post.ip'));
        $ip0= trim(X('post.ip0'));
        $mask = trim(X("post.mask"));
        $gateway = trim(X("post.gateway"));
        //先修改配置文件 如果失败 直接返回
        $content = @file_get_contents(INDEX_PATH . 'Conf/config.php');
        if($content === false)
            $this->json(array('error'=>false,'info'=>'/Conf读取失败'));
        $pose_DOMAIN_NAME =  strpos($content,"'DOMAIN_NAME' =>");
        $pose_dou1 = strpos($content,"'",$pose_DOMAIN_NAME+17);
        $pose_dou2 = strpos($content,"'",$pose_dou1+1);
        $dn_old=substr($content,$pose_dou1,intval($pose_dou2-$pose_dou1+1));
        $content = str_replace($dn_old,"'http://".$ip."'",$content);



        if(@file_put_contents(INDEX_PATH . 'Conf/config.php',$content) === false)
            $this->json(array('error'=>false,'info'=>'/Conf无写入失败'));

        exec('netsh interface ip set address "worker" static '.$ip.' '.$mask.' '.$gateway);
        $this->json(["error"=>true,"info"=>"已修改" ,"content"=>$content]);
    }
    public function index(){
        $preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
        //获取操作系统为win2000/xp、win7的本机IP真实地址
        $res=[];
        exec("ipconfig", $out, $stats);
        if (!empty($out)) {
            foreach ($out AS $k=>$row) {
                if(strstr($row, "worker:")) {
                    while(1){
                        $k++;
                        if(!isset($out[$k])){
                            break;
                        }
                        //如果到了下一个适配器 则退出
                        if(strstr($out[$k], ":")&&!strstr($out[$k], ". :")){
                            break;
                        }
                        if (strstr($out[$k], "IP") && strstr($out[$k], ":") && !strstr($out[$k], "IPv6")) {
                            //防止溢出 超过就退出循环

                            $tmpIp = explode(":", $out[$k]);
                            if (preg_match($preg, trim($tmpIp[1]))) {
                                array_push($res,trim($tmpIp[1]));
                            }else{
                                array_push($res,"");
                            }
                            $k++;
                            $tmpIp = explode(":", $out[$k]);
                            if (preg_match($preg, trim($tmpIp[1]))) {
                                array_push($res,trim($tmpIp[1]));
                            }else{
                                array_push($res,"");
                            }
                            $k++;
                            $tmpIp = explode(":", $out[$k]);
                            if (preg_match($preg, trim($tmpIp[1]))) {
                                array_push($res,trim($tmpIp[1]));
                            }else{
                                array_push($res,"");
                            }
                            break;
                        }
                    }
                }
            }
        }
        //$out = $Kses->Parse($out[3]);
        //$this->json($res);
        //$this->json([gettype($out),sizeof($out[3])]);
        $res[0]=isset($res[0])?$res[0]:"";
        $res[1]=isset($res[1])?$res[1]:"";
        $res[2]=isset($res[2])?$res[2]:"";
        $this->v('netinfo',$res);
        $this->view = 'CQQ';
        $this->v("title","画方");
        $this->v("title2","全流量加速系统");
        $this->display('setip');
    }
	
	
	//{hook a_index_fun}
}
